package com.example.teacher.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.Section;
import com.example.common.utils.CacheClient;
import com.example.teacher.Service.SectionService;
import com.example.teacher.mapper.SectionMapper;
import jakarta.persistence.EntityNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SectionServiceImpl extends ServiceImpl<SectionMapper, Section> implements SectionService {

    @Autowired
    private SectionMapper sectionMapper;
    @Autowired
    private CacheClient cacheClient;

//    @Override
//    public Handouts getHandoutsById(String HandoutsId){
//        return handoutsMapper.selectById(HandoutsId);
//    }

    @Override
    public Section getSectionBySectionId(String sectionId) {
        // 使用逻辑过期策略获取小节信息
        return cacheClient.queryWithLogicalExpire(
                RdsConstants.SECTION_INFO,
                sectionId,
                Section.class,
                this::getSectionById, // 使用MyBatis-Plus的基础方法
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES,
                false
        );
    }

    @Override
    public Section getSectionById(String id) {
        // 构建查询条件
        LambdaQueryWrapper<Section> queryWrapper = new LambdaQueryWrapper<>();
        // 设置查询条件：根据 sectionId 字段查询
        queryWrapper.eq(Section::getId, id);
        // 执行查询并返回结果
        return sectionMapper.selectOne(queryWrapper);
    }

    @Override
    public Section getSectionByIdSql(String sectionId) {
        // 构建查询条件
        LambdaQueryWrapper<Section> queryWrapper = new LambdaQueryWrapper<>();
        // 设置查询条件：根据 sectionId 字段查询
        queryWrapper.eq(Section::getSectionId, sectionId);
        // 执行查询并返回结果
        return sectionMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveSection(Section section) {
        sectionMapper.insert(section);
    }

    @Override
    public void updateSection(Section section) {
        // 参数校验：确保复合主键存在
        if (section.getId() == null) {
            log.info("更新小节时必须提供 id");
            throw new IllegalArgumentException("更新小节时必须提供 id");
        }
        // 构建 UpdateWrapper，使用 Lambda 表达式避免硬编码字段名
        UpdateWrapper<Section> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件（复合主键）
        updateWrapper.lambda()
                .eq(Section::getId, section.getId());
        // 动态设置需要更新的字段（只更新非 null 值）
        LambdaUpdateWrapper<Section> lambdaUpdate = updateWrapper.lambda();
        if (section.getSectionId() != null) {
            lambdaUpdate.set(Section::getSectionId, section.getSectionId());
        }
        if (section.getSectionName() != null) {
            lambdaUpdate.set(Section::getSectionName, section.getSectionName());
        }
        if (section.getPptId() != null) {
            lambdaUpdate.set(Section::getPptId, section.getPptId());
        }
        if (section.getHandoutsId() != null) {
            lambdaUpdate.set(Section::getHandoutsId, section.getHandoutsId());
        }
        // 执行更新操作
        int rows = sectionMapper.update(null, updateWrapper);
        // 检查更新结果，失败时抛出异常
        if (rows == 0) {
            log.warn("更新小节失败，未找到记录，id={}, courseId={}", section.getId(), section.getCourseId());
            throw new EntityNotFoundException("未找到对应小节记录，id=" + section.getId() + ", courseId=" + section.getCourseId());
        }
    }


}
